<template>
  <div class="register-container">
    <h2>注册</h2>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label>用户名</label>
        <input v-model="username" type="text" required />
      </div>
      <div class="form-group">
        <label>密码</label>
        <input v-model="password" type="password" required />
      </div>
      <div class="form-group">
        <label>确认密码</label>
        <input v-model="confirmPassword" type="password" required />
      </div>
      <button type="submit">注册</button>
      <p class="to-login">已有账号？<router-link to="/login">去登录</router-link></p>
    </form>
  </div>
</template>

<script>
import { userApi } from '@/utils/request'
export default {
  name: 'Register',
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    }
  },
  methods: {
    handleRegister() {
      if (this.password !== this.confirmPassword) {
        alert('两次密码不一致')
        return
      }
      userApi.register(this.username, this.password, this.confirmPassword).then(res => {
        // 注册成功逻辑
        this.$router.push('/login')
      }).catch(() => {
        alert('注册失败')
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  max-width: 350px;
  margin: 60px auto;
  padding: 30px;
  border: 1px solid #eee;
  border-radius: 8px;
  background: #fff;
}
.form-group {
  margin-bottom: 18px;
}
input {
  width: 100%;
  padding: 8px;
  margin-top: 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
button {
  width: 100%;
  padding: 10px;
  background: #42b983;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
}
.to-login {
  margin-top: 16px;
  text-align: right;
}
</style>